import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const message 	= $Lazy.file(()=>import('@antd/message'))
const Printer 	= $Lazy.hook(()=>import('#private/printer'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '条码号',	name: 'spec_code',	width:'25%',	max:14,	min:12, autoFocus:true,
				onPressEnter: value => {
					if(value){
						if(value.length === 12 || value.length === 14){
							this.fetch().then((data)=>{
								if($fn.hasArray(data)){
									this.searchFormRef.setValue({spec_code: ''})
									setTimeout(() => document.querySelector('#spec_code input').focus(), 500)
									this.printerRef.printer({ code: 50104, data:data, loading:false})
								}
							})
						}else{
							message.then(f=>f.error(`请输入12或14位条码号`,1.5))
						}
					}
				}
			},
			{ label: '科室',	name: 'team_id',	type:'select',	loading:true, 	idStr:'uuid' },
		],
	}
	
	componentDidMount(){
		this.getSelect()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 科室
			$fn.setRow( forms, 'team_id', await f.getCache({ api:'ke_shi' }) )
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'specimen/printspec',{paging:'get', isIndex: true} )
	// 表单
	cols = [
		{ title: '主条码', 		dataIndex: 'old_spec_code', 	...$fn.table.spec_code },
		{ title: '子条码', 		dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '科室', 		dataIndex: 'team_name', 		width:100 },
		{ title: '岗位', 		dataIndex: 'job_id', 			width:100 },
		{ title: '实验号', 		dataIndex: 'lb_tpl', 			...$fn.table.spec_code },
		{ title: '自然项目', 	dataIndex: 'kind_name', 		width:150 },
		{ title: '患者姓名', 	dataIndex: 'patient_name', 		...$fn.table.name },
		{ title: '接收时间', 	dataIndex: 'sp_receive_time',  	...$fn.table.time},
	]
	
	// 操作按钮
	controls = () => {
		return [
			Controls.printer.call(this, {label:'打印实验号',code: 50104, batch:true,} )			 	// 打印实验号
		]
	}
		
	render(){
		return (
			<Page title='按条码打印实验号'  controls={this.controls()}>
				{/* 搜索 */}
				{ SearchForm.call(this) }
				{/* 表格 */}
				{ Table.call(this, { isIndex: true }) }
				{/* 打印 */}
				<Printer onRef = { ref => this.printerRef = ref } hasBtn = {false} loading = {false} />
			</Page>
		)
	}
}

export default Index